<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/28
 * Time: 15:55
 */

namespace YMApi\Service;


use YMApi\Repositories\BuyNowGoodsRepository;

/**
 * Class BuyNowService
 * @package SDXApi\Service
 */
class BuyNowGoodsService
{
	/**
	 * @var BuyNowGoodsRepository
	 */
	protected static $buyNowGoodsRepository;

	/**
	 * @var
	 */
	protected $userId;

	/**
	 * @var
	 */
	protected $wid;

	/**
	 * @var array
	 */
	protected $message = array();

	/**
	 * @var \Model|\Think\Model
	 */
	protected static $orderModel;

	/**
	 * @var
	 */
	protected static $orderPromoteNumber;

	/**
	 * @var
	 */
	protected $errorMessage;


	/**
	 * 注入实例
	 * BuyNowService constructor.
	 * @param $userId
	 * @param $wid
	 */
	public function __construct($userId, $wid = 8)
	{
		self::$buyNowGoodsRepository = new BuyNowGoodsRepository($userId, $wid);
		self::$orderModel = D('Orderinfo');

		$this->userId = $userId;
		$this->wid = $wid;
	}


	/**
	 * 检测该商品是否为秒杀商品
	 * Created by Jerry
	 * @param $goodsId
	 * @param $goodsNumber
	 * @return bool
	 */
	public function isBuyNowGoods($goodsId, $goodsNumber)
	{

		$goodsInfo = self::$buyNowGoodsRepository->findBuyNowGoodsInfoByGoodsId($goodsId);

		return $this->checkBuyNowGoods($goodsInfo, $goodsNumber);
	}

	/**
	 * Created by Jerry
	 * @param $goods
	 * @return bool|mixed
	 */
	public function getBuyNowGoodsDataFormat($goods)
	{
		if (empty($goods)) {
			return false;
		}

		return self::$buyNowGoodsRepository->getBuyNowGoodsData($goods);
	}



	/**
	 * Created by Jerry
	 * @param $goods
	 * @return bool
	 */
	public function getBuyNowGoodsPrice($goods)
	{
		return self::$buyNowGoodsRepository->checkBuyNowPrice($goods);
	}

	/**
	 * 检查秒杀商品并处理逻辑
	 * Created by Jerry
	 * @param $goodsInfo
	 * @param $goodsNumber
	 * @return bool
	 */
	protected function checkBuyNowGoods($goodsInfo, $goodsNumber)
	{
		if (empty($goodsInfo) || empty($goodsNumber)) {
			return false;
		}

		$todayCount = $this->findOrderBuyNowGoodsByToDay($this->userId, $goodsInfo['goods_id']);

		$orderBuyNowGoodsCount = $this->findOrderBuyNowGoodsByNumberTime($this->userId, $goodsInfo);

		return $this->thenBuyNowGoods($goodsInfo, $todayCount, $orderBuyNowGoodsCount, $goodsNumber);
	}

	/**
	 * 处理秒杀商品错误信息
	 * Created by Jerry
	 * @param $goodsInfo
	 * @param $toDayNumber
	 * @param $orderBuyNowGoodsCount
	 * @param $goodsNumber
	 * @return bool
	 */
	protected function thenBuyNowGoods($goodsInfo, $toDayNumber, $orderBuyNowGoodsCount, $goodsNumber)
	{
		$time = gmtime();

		if (($goodsInfo['promote_start_date'] > 0 && $goodsInfo['promote_end_date'] > 0)) {
			if (($goodsInfo['promote_start_date'] < $time &&  $goodsInfo['promote_end_date'] < $time)) {
//			秒杀商品已过期
				$this->errorMessage = '秒杀商品已过期，请重试';
				return true;
			}

			if ($goodsInfo['promote_start_date'] > $time &&  $goodsInfo['promote_end_date'] > $time) {
//			秒杀商品未开始
				$this->errorMessage = '秒杀商品未开始，请重试';
				return true;
			}
		}

//		限购商品数量
		if ($goodsInfo['promote_goods_number'] > 0 && $goodsInfo['promote_goods_number'] < $goodsNumber) {
			$this->errorMessage = '此秒杀商品已超出单次购买数量超过上限';
			return false;
		}

//		每天限购数量
		if ($goodsInfo['promote_today_number'] > 0 && $goodsInfo['promote_today_number'] <= $toDayNumber) {
			$this->errorMessage = '此秒杀商品已超出当天可购买数量';
			return false;
		}

		if ($goodsInfo['promote_today_number'] < 0 && abs($goodsInfo['promote_today_number']) <= $orderBuyNowGoodsCount) {
			$this->errorMessage = '此秒杀商品已超出此次促销活动可购买数量';
			return false;
		}

		return true;
	}

	/**
	 * 获取指定用户在该活动期间内该商品订单总数
	 * Created by Jerry
	 * @param $uid
	 * @param $goodsInfo
	 * @return mixed
	 */
	protected function findOrderBuyNowGoodsByNumberTime($uid, $goodsInfo)
	{
		return self::$orderModel->get_order_buy_now_by_number($uid, $goodsInfo['goods_sn'], $goodsInfo['promote_start_date'], $goodsInfo['promote_end_date']);
	}

	/**
	 * 返回当天此会员秒杀的单个商品订单总数
	 * Created by Jerry
	 * @param $userId
	 * @param $goodsId
	 * @return mixed
	 */
	protected function findOrderBuyNowGoodsByToDay($userId, $goodsId)
	{
		return self::$orderModel->get_order_buy_now_by_to_day_for_count($userId, $goodsId);
	}

	/**
	 * 处理秒杀商品数据字段
	 * Created by Jerry
	 * @param $goods
	 * @return bool|mixed
	 */
	public function handleBuyNowGoodsData($goods)
	{
		if (empty($goods)) {
			return false;
		}

		$data = self::$buyNowGoodsRepository->getBuyNowGoodsData($goods);

		$data['buy_now_message'] = $this->thenBuyNowGoodsLimitMessage($data)->getLimitMessage();

		unset($data['promote_end_date'], $data['promote_start_date'], $data['promote_goods_number'], $data['promote_today_number'], $data['promote_price']);

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param $goods
	 * @return bool|mixed
	 */
	public function handleBuyNowGoodsCategoryData($goods)
	{
		if (empty($goods)) {
			return false;
		}

		$data = self::$buyNowGoodsRepository->getBuyNowGoodsData($goods);

		$data['buy_now_message'] = $this->thenBuyNowGoodsLimitMessage($data)->getLimitMessage();

//		unset($data['promote_end_date'], $data['promote_start_date'], $data['promote_goods_number'], $data['promote_today_number'], $data['promote_price']);

		return $data;
	}


	/**
	 * Created by Jerry
	 * @param $goods
	 * @return bool
	 */
	public function handleBuyNowGoodsDataByCart($goods)
	{
		$data = $this->handleBuyNowGoodsData($goods);

		if (empty($data)) {
			return false;
		}

//		处理秒杀中的商品
		if ($data['buy_now_status'] == 3 && $data['buy_now_goods_number'] > 0) {
			$data = $this->checkBuyNowGoodsForCart($data);
		}

		return $data;
	}

	/**
	 * 处理购物车秒杀商品
	 * Created by Jerry
	 * @param $goods
	 * @return bool
	 */
	protected function checkBuyNowGoodsForCart($goods)
	{
		$goodsInfo = self::$buyNowGoodsRepository->findBuyNowGoodsInfoByGoodsId($goods['goods_id']);

		if (empty($goodsInfo)) {
			return false;
		}

		$updateData = array();
		if ($goodsInfo['promote_price'] > 0 && ($goodsInfo['promote_price'] - $goods['goods_price'] != 0)) {
			$goods['goods_price'] = $updateData['goods_price'] = $goodsInfo['promote_price'];
		}

		if ($goods['giving'] == 0) {
			$goods['giving'] = $updateData['giving'] = 5;
		}

		if ($goods['goods_number'] > $goodsInfo['promote_goods_number']) {
			$goods['goods_number'] = $updateData['goods_number'] = $goodsInfo['promote_goods_number'];
		}

		if (!empty($updateData)) {
			$this->updateBuyNowGoodsCartDataByCartId($goods['rec_id'], $updateData);
		}

		return $goods;
	}

	/**
	 * Created by Jerry
	 * @param $cartId
	 * @param $updateData
	 * @return bool
	 */
	protected function updateBuyNowGoodsCartDataByCartId($cartId, $updateData)
	{
		if (empty($cartId) || empty($updateData)) {
			return false;
		}

		return self::$buyNowGoodsRepository->updateBuyNowGoodsCartDataByCartId($cartId, $updateData);
	}

	/**
	 * 处理秒杀商品限购信息
	 * Created by Jerry
	 * @param $goods
	 * @return $this
	 */
	protected function thenBuyNowGoodsLimitMessage($goods)
	{
		$time = gmtime();
		if ($goods['promote_start_date'] < $time &&  $goods['promote_end_date'] < $time) {
//			秒杀商品已过期
			array_push($this->message, '');
			return $this;
		}

		if ($goods['promote_start_date'] > $time &&  $goods['promote_end_date'] > $time) {
//			秒杀商品未开始
			array_push($this->message, '');
			return $this;
		}

		if ($goods['promote_goods_number'] > 0) {
			array_push($this->message, '每单限购' . $goods['promote_goods_number'] . '份');
		}

		if ($goods['promote_today_number'] > 0) {
			array_push($this->message, '每天限购' . $goods['promote_today_number'] . '单');
		}

		if ($goods['promote_today_number'] < 0) {
			array_push($this->message, '每人限购' .abs($goods['promote_today_number']). '单');
		}

		return $this;
	}

	/**
	 * 获取限制信息
	 * Created by Jerry
	 * @return string
	 */
	public function getLimitMessage()
	{
		$message = implode(',', $this->message);
		return trim($message, ',');
	}

	/**
	 * Created by Jerry
	 * @return mixed
	 */
	public function getErrorMessage()
	{
		return $this->errorMessage;
	}
}